#!/bin/bash

# FZF software management script
# This script provides functions for managing fzf (fuzzy finder) installation

# Default configuration
FZF_VERSION=${fzf_version:-"0.66.1"}
FZF_ARCH="linux_amd64"
FZF_NAME="fzf-${FZF_VERSION}-${FZF_ARCH}"
FZF_ENV_VAR="FZF_HOME"
FZF_DOWNLOAD_PATH="/tmp/fzf-download"
PROXY_PREFIX=${proxy_prefix:-"https://ghproxy.net/"}

# Get software information
get_software_info() {
  cat <<EOF
Software Name: fzf
Description: A command-line fuzzy finder
Version: ${FZF_VERSION}
Architecture: ${FZF_ARCH}
Homepage: https://github.com/junegunn/fzf
Installation Path: ${softwares_path}/${FZF_NAME}
Environment Variable: ${FZF_ENV_VAR}
EOF
}

# Download and install fzf
execute_install_software() {
  local softwares_path="$1"
  local fzf_home="${softwares_path}/${FZF_NAME}"

  echo "Starting fzf installation..."
  echo "Version: ${FZF_VERSION}"
  echo "Architecture: ${FZF_ARCH}"
  echo "Installation path: ${fzf_home}"

  # Create download directory
  mkdir -p "${FZF_DOWNLOAD_PATH}"

  # Download fzf
  local download_url="${PROXY_PREFIX}https://github.com/junegunn/fzf/releases/download/v${FZF_VERSION}/${FZF_NAME}.tar.gz"
  echo "Downloading fzf ${FZF_VERSION}..."
  echo "Download URL: $download_url"
  curl -C - -o "${FZF_DOWNLOAD_PATH}/${FZF_NAME}.tar.gz" -L0 "$download_url"

  if [[ $? -ne 0 ]]; then
    echo "Error: Failed to download fzf"
    return 1
  fi

  # Create software directory
  sudo mkdir -p "${softwares_path}"

  # Remove existing installation if it exists
  if [[ -d "${fzf_home}" ]]; then
    echo "Removing existing installation..."
    sudo rm -rf "${fzf_home}"
  fi

  # Create directory and extract binary
  echo "Installing fzf to ${fzf_home}..."
  sudo mkdir -p "${fzf_home}"
  sudo tar -C "${fzf_home}" -xzf "${FZF_DOWNLOAD_PATH}/${FZF_NAME}.tar.gz"

  if [[ $? -eq 0 ]]; then
    # Move binary from temporary location to the directory
    if [[ -f "${softwares_path}/fzf" ]]; then
      sudo mv "${softwares_path}/fzf" "${fzf_home}/fzf"
    fi
    
    # Ensure proper permissions
    sudo chmod +x "${fzf_home}/fzf"
    
    echo "fzf installation completed successfully"
    return 0
  else
    echo "Error: Failed to install fzf"
    return 1
  fi
}

# Set environment configuration
set_rc_config() {
  local rc_path="$1"
  local fzf_home="${softwares_path}/${FZF_NAME}"

  cat <<EOF

# ${FZF_ENV_VAR} configuration
${FZF_ENV_VAR}="${fzf_home}"
export PATH="\${PATH}:\${${FZF_ENV_VAR}}"
EOF
}

# Uninstall fzf
execute_uninstall_software() {
  local softwares_path="$1"
  local fzf_home="${softwares_path}/${FZF_NAME}"

  echo "Starting fzf uninstallation..."

  # Remove installation directory
  if [[ -d "${fzf_home}" ]]; then
    echo "Removing fzf installation directory: ${fzf_home}"
    sudo rm -rf "${fzf_home}"
  else
    echo "fzf installation directory not found: ${fzf_home}"
  fi

  # Remove download cache
  if [[ -d "${FZF_DOWNLOAD_PATH}" ]]; then
    echo "Cleaning download cache: ${FZF_DOWNLOAD_PATH}"
    rm -rf "${FZF_DOWNLOAD_PATH}"
  fi

  echo "fzf uninstallation completed"
  return 0
}

# Check if fzf is installed
check_installed() {
  local softwares_path="$1"
  local fzf_home="${softwares_path}/${FZF_NAME}"

  if [[ -d "${fzf_home}" ]] && [[ -x "${fzf_home}/fzf" ]]; then
    echo "fzf is installed at: ${fzf_home}"
    echo "Version: $("${fzf_home}/fzf" --version 2>/dev/null | head -n1)"
    return 0
  else
    echo "fzf is not installed"
    return 1
  fi
}

# Main function for direct script execution
main() {
  if [[ "$1" == "info" ]]; then
    get_software_info
  elif [[ "$1" == "install" ]]; then
    execute_install_software "${softwares_path:-/opt/softwares}"
  elif [[ "$1" == "uninstall" ]]; then
    execute_uninstall_software "${softwares_path:-/opt/softwares}"
  elif [[ "$1" == "check" ]]; then
    check_installed "${softwares_path:-/opt/softwares}"
  else
    echo "Usage: $0 {info|install|uninstall|check}"
    exit 1
  fi
}

# If script is executed directly, run main function
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
  main "$@"
fi
